package com.hbird.mapper;

import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.hbird.entity.TUser;
import com.hbird.entity.TUserExample;

import java.lang.reflect.Field;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

@Repository
public interface TUserMapper {
    ////////////////////这个方式我自己加的//////////////////
    List<TUser> selectAllUser();

    //为了解决对象属性和字段驼峰不一致的问题，我们可以使用映射注解@Results来指定映射关系。
    @Results({
            @Result(property = "userId", column = "user_id"),
            @Result(property = "userName", column = "user_name"),
            @Result(property = "password", column = "password"),
            @Result(property = "phone", column = "phone")
    })
    @Select("SELECT * FROM t_user")
    List<TUser> listAllUser();

    @Select("SELECT * FROM t_user WHERE id = #{id}")
    TUser getOne(Long id);

    @Select("select * from t_user where user_name like #{username}")
    List<TUser> findByUsername(String username);

    /**
     * 1.用于获取结果集的映射关系
     */
    public static String getResultsStr(Class origin) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("@Results({\n");
        for (Field field : origin.getDeclaredFields()) {
            String property = field.getName();
            //映射关系：对象属性(驼峰)->数据库字段(下划线)
            String column = new PropertyNamingStrategy.SnakeCaseStrategy().translate(field.getName()).toUpperCase();
            stringBuilder.append(String.format("@Result(property = \"%s\", column = \"%s\"),\n", property, column));
        }
        stringBuilder.append("})");
        return stringBuilder.toString();
    }

    ///////////////////////////////////////////////////////

    int countByExample(TUserExample example);

    int deleteByExample(TUserExample example);

    int deleteByPrimaryKey(Integer userId);

    int insert(TUser record);

    int insertSelective(TUser record);

    List<TUser> selectByExample(TUserExample example);

    TUser selectByPrimaryKey(Integer userId);

    int updateByExampleSelective(@Param("record") TUser record, @Param("example") TUserExample example);

    int updateByExample(@Param("record") TUser record, @Param("example") TUserExample example);

    int updateByPrimaryKeySelective(TUser record);

    int updateByPrimaryKey(TUser record);
}